import * as Blockly from 'blockly/core';

const NETWORK_HUE = '#5B6DA5';

export const network_init = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .appendField("")
            .setCheck("var");
        this.appendDummyInput()
            .appendField("初始化");
        this.appendDummyInput()
            .appendField("模式")
            .appendField(new Blockly.FieldDropdown([
                ['标准', "2"],
                ['快速', "1"]
            ]), "mode");
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip("[Teaile]:初始化WiFi设置，可设置标准及慢速模式");
    }
};

export const network_scan = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .appendField("")
            .setCheck("var");
        this.appendDummyInput()
            .appendField("WiFi扫描");
        this.setOutput(true);
        this.setInputsInline(true);
        this.setTooltip("[Teaile]:扫描附近可用的WiFi信息-返回多信息列表");
    }
};

export const network_connect = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .appendField("")
            .setCheck("var");
        this.appendDummyInput("")
            .appendField("WiFi连接");
        this.appendValueInput('account')
            .appendField("名称")
            .setCheck(String);
        this.appendValueInput('password')
            .appendField("密码")
            .setCheck(String);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip("[Teaile]:配置WiFi名称及密码，连接WiFi上网");
    }
};

export const network_ifconfig = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .appendField("")
            .setCheck("var");
        this.appendDummyInput()
            .appendField("WiFi")
            .appendField(new Blockly.FieldDropdown([
                ['连接信息', "1"],
                ['连接状态', "2"]
            ]), "mode");
        this.setOutput(true);
        this.setInputsInline(true);
        this.setTooltip("[Teaile]:查看wifi连接信息或者连接状态");
    }
};

export const network_disconnect = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .appendField("")
            .setCheck("var");
        this.appendDummyInput()
            .appendField("WiFi断开连接");
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip("[Teaile]:断开wifi连接");
    }
};

export const network_enable_ap = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .appendField("")
            .setCheck("var");
        this.appendDummyInput("")
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("打开热点");
        this.appendValueInput('account')
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("名称")
            .setCheck(String);
        this.appendValueInput('password')
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("密码")
            .setCheck(String);
        this.appendValueInput('chl')
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("通道")
            .setCheck(Number);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("加密")
            .appendField(new Blockly.FieldDropdown([
                ['WPA2_PSK', "WPA2_PSK"],
                ['OPEN', "OPEN"],
                ['WPA_PSK', "WPA_PSK"],
                ['WPA_WPA2_PSK', "WPA_WPA2_PSK"]
            ]), "mode");
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip("[Teaile]:打开WiFi热点并配置");
    }
};

export const network_disable_ap = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .appendField("")
            .setCheck("var");
        this.appendDummyInput()
            .appendField("关闭热点");
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip("[Teaile]:关闭热点");
    }
};



//ok
export const network_socket_init = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_INIT)
            .setCheck("var");
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_TYPE)
            .appendField(new Blockly.FieldDropdown([
                ['TCP', "TCP"],
                ['UDP', "UDP"]
            ]), "mode");
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setInputsInline(true);
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('mode');
            var mode0 = Blockly.Msg.MIXLY_ESP32_NETWORK_SOCKET_INIT_TOOLTIP
            var TOOLTIPS = {
                'TCP': 'TCP',
                'UDP': 'UDP',
            };
            return mode0 + TOOLTIPS[mode]
        });
    }
};

//ok
export const network_socket_getaddrinfo = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .appendField("")
            .setCheck("var");
        this.appendDummyInput()
            .appendField("获取地址");
        this.appendValueInput('addr')
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("域名")
            .setCheck(String);
        this.setOutput(true);
        this.setInputsInline(true);
        this.setTooltip("[Teaile]:套接字根据域名解析地址IP信息");
    }
};

//ok
export const network_socket_bind = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .setCheck("var")
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_LET_SOCKET)
        this.appendValueInput('address')
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_BIND_TO_ADDRESS);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_ESP32_NETWORK_SOCKET_BIND_TOOLTIP);
    }
};

export const network_socket_connect = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .setCheck("var")
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_LET_SOCKET)
        this.appendValueInput('address')
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_CONNECT_TO_ADDRESS);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_ESP32_NETWORK_SOCKET_CONNECT_TOOLTIP);
    }
};

export const network_socket_listen = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .setCheck("var")
        this.appendValueInput('queue')
            .setCheck(Number)
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_LISTEN)
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_QUEUE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_ESP32_NETWORK_SOCKET_LISTEN_TOOLTIP);
    }
};

export const network_socket_accept = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .setCheck("var")
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_ACCEPT);
        this.setOutput(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_ESP32_NETWORK_SOCKET_ACCEPT_TOOLTIP);
    }
};

export const network_socket_receive = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .setCheck("var")
            .appendField("TCP")
        this.appendValueInput('size')
            .setCheck(Number)
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_RECEIVE)
        this.setOutput(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_ESP32_NETWORK_SOCKET_RECEIVE_TOOLTIP);
    }
};

export const network_socket_send = {
    init: function () {
        this.setColour(NETWORK_HUE);
        // .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_MAKE)
        this.appendValueInput('VAR')
            .setCheck("var")
            .appendField("TCP")
        this.appendValueInput('content')
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_SEND)
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_ESP32_NETWORK_SOCKET_SEND_TOOLTIP);
    }
};

export const network_socket_receive_from = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .setCheck("var")
            .appendField("UDP")
        this.appendValueInput('size')
            .setCheck(Number)
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_RECEIVE)
        this.setOutput(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_ESP32_NETWORK_SOCKET_RECEIVE_FROM_TOOLTIP);
    }
};

export const network_socket_send_to = {
    init: function () {
        this.setColour(NETWORK_HUE);
        // .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_MAKE)
        this.appendValueInput('VAR')
            .setCheck("var")
            .appendField("UDP")
        this.appendValueInput('content')
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_SEND)
        this.appendValueInput('address')
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_TO);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_ESP32_NETWORK_SOCKET_SEND_TO_TOOLTIP);
    }
};

export const network_socket_close = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendValueInput('VAR')
            .setCheck("var")
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_CLOSE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_ESP32_NETWORK_SOCKET_CLOSE_TOOLTIP);
    }
};

//---------------TABIT WiFi ESP32资源---20230520--------------------

export const network_tabit_scan = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendDummyInput()
            .appendField("WiFi扫描");
        this.setOutput(true);
        this.setInputsInline(true);
        this.setTooltip("[Teaile]：扫描附近可用的WiFi信息-返回多信息列表");
    }
};

export const network_tabit_connect = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendDummyInput("")
            .appendField("WiFi 连接");
        this.appendValueInput('account')
            .appendField("名称")
            .setCheck(String);
        this.appendValueInput('password')
            .appendField("密码")
            .setCheck(String);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip("[Teaile]：配置WiFi名称及密码，连接WiFi上网");
    }
};

export const network_tabit_ifconfig = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendDummyInput()
            .appendField("WiFi ")
            .appendField(new Blockly.FieldDropdown([
                ['连接信息', "1"],
                ['连接状态', "2"]
            ]), "mode");
        this.setOutput(true);
        this.setInputsInline(true);
        this.setTooltip("[Teaile]：查看wifi连接信息或者连接状态");
    }
};

export const network_tabit_disconnect = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendDummyInput()
            .appendField("WiFi 断开连接");
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip("[Teaile]：断开wifi连接");
    }
};

export const network_tabit_ping = {
    init: function () {
        this.setColour(NETWORK_HUE);
        this.appendDummyInput("")
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("WiFi Ping网络");
        this.appendValueInput('ping')
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("地址")
            .setCheck(String);
        this.setOutput(true);
        this.setInputsInline(true);
        this.setTooltip("[Teaile]：ping网络地址，返回时间");
    }
};